// pages/share/material.js
const app = getApp();

import {
  writePhotosAlbum
} from '../../utils/util'

import {
  IncomeModels
} from "../../utils/models/income/Income-models";
var incomeModel = new IncomeModels;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    writing: '澳大利亚原装进口，纯天然保健品',
    dynamic_list: [{
      img_url: 'https://www.allfighting.top/uploads/images/20200817/1708b27fbfb562df435021dba40715cf.jpg',
      select: true
    }, {
      img_url: 'https://www.allfighting.top/uploads/images/20200817/929eb1e9cfec0952fc4cdb9ce1e9a24d.jpg',
      select: true
    }, {
      img_url: 'https://www.allfighting.top/uploads/images/20200818/be91ccfd1d9386c31f0b00fb38f0fd2b.jpg',
      select: true
    }, {
      img_url: 'https://www.allfighting.top/uploads/images/20200814/7c353d05e486952bde7c5c79d0a39b37.jpg',
      select: true
    }, {
      img_url: 'https://www.allfighting.top/uploads/images/20200817/41f3a7874193f5b1a01c197e4b274608.jpg',
      select: true
    }],
    income:0,
    dynamic_list2: [],
    selectAll: true,
    percent: 0,
    schedule: false,
    gid:0,
  },

  // 点击图片 选择(反选)
  onSelect: function(e){
    var index = e.currentTarget.dataset.index;
    var dynamic_list = this.data.dynamic_list;
    dynamic_list[index].select = !dynamic_list[index].select;
    
    var selectAll = true;
    for(var i=0;i<dynamic_list.length;i++) {
      if(!dynamic_list[i].select) {
        selectAll = false;
        break;
      }
    }
    this.setData({
      dynamic_list: dynamic_list,
      selectAll: selectAll
    });
  },

  // 全选(全不选)
  selectAll: function() {
    var dynamic_list = this.data.dynamic_list;
    if(this.data.selectAll) {
      //全选改全不选
      var selectAll = false;
      for(var i=0;i<dynamic_list.length;i++) {
        dynamic_list[i].select = false;
      }
    }else {
      //全不选改全选
      var selectAll = true;
      for(var i=0;i<dynamic_list.length;i++) {
        dynamic_list[i].select = true;
      }
    }

    this.setData({
      dynamic_list: dynamic_list,
      selectAll: selectAll
    })
  },

  // 下载图片到本地相册
  download(e) {
    var dynamic_list = this.data.dynamic_list;
    var dynamic_list2 = []; //创建新数组，放需要保存的图片信息
    for(var i=0;i<dynamic_list.length;i++) {
      if(dynamic_list[i]['select']) {
        dynamic_list2.push(dynamic_list[i]);
      }
    }
    console.log(`dynamic_list`,dynamic_list)
    console.log(`dynamic_list2`,dynamic_list2)
    this.setData({
      dynamic_list2: dynamic_list2
    },function(){
      this.getsave(0, this.data.dynamic_list2.length)
    });
  },

  getsave(i, length) {
    wx.showLoading({
      title: '下载中(' + (i + 1) + '/' + length + ')',
    })
    const downloadTask = wx.downloadFile({
      url: this.data.dynamic_list2[i].img_url,
      success: (res) => {
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: (res) => {
            if (i + 1 == length) {
              wx.showToast({
                title: '保存成功',
              });
            }
            wx.hideLoading()
            if (++i < length) {
              this.getsave(i, length);
            }
          },
          fail: (err) => {
            wx.showToast({
              title: '保存图片失败',
              icon: 'none',
            })
          },
        })
      },
    })
    // 下载进度
    downloadTask.onProgressUpdate((res) => {
      console.log(res)
      if (res.progress > 0) {
        this.setData({
          schedule: true,
          percent: res.progress
        })
      }
      if (res.progress == 100) {
        this.setData({
          schedule: false
        })
      }
    })
  },
  getSource:function(gid){
    var that = this;
    incomeModel.getSource(gid,function(res){
      that.setData({
        writing: res.data.share_goods.share_text,
        income:res.data.share_goods.income,
        dynamic_list: res.data.share_goods.source
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var gid = options.id;
    console.log(gid);
    this.getSource(gid);
    this.setData({
      gid:gid
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})